package com.mashibing.apipassenger.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.mashibing.apipassenger.feign.ServicePassengerUserFeign;
import com.mashibing.apipassenger.feign.ServiceVerificationCodeFeign;
import com.mashibing.apipassenger.service.VerificationCodeService;
import com.mashibing.internalcommon.constant.CommonStatusEnum;
import com.mashibing.internalcommon.constant.IdentityConstants;
import com.mashibing.internalcommon.constant.TokenConstants;
import com.mashibing.internalcommon.dto.ResponseResult;
import com.mashibing.internalcommon.request.VerificationCodeDTO;
import com.mashibing.internalcommon.response.NumberCodeResponse;
import com.mashibing.internalcommon.response.TokenResponse;
import com.mashibing.internalcommon.utils.JwtUtils;
import com.mashibing.internalcommon.utils.RedisPrefixUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @title: VerificationCodeServiceImpl
 * @Author Z先森
 * @Date: 2022/9/19 11:45
 * @Version 1.0
 */
@Service
@Slf4j
public class VerificationCodeServiceImpl implements VerificationCodeService {
    @Autowired
    private ServiceVerificationCodeFeign serviceVerificationCodeFeign;
    @Autowired
    private ServicePassengerUserFeign servicePassengerUserFeign;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public ResponseResult generatorCode(String passengerPhone) {

        //1.获取验证码
        ResponseResult<NumberCodeResponse> result = serviceVerificationCodeFeign.generatorNumberCode(6);
        int numberCode = result.getData().getNumberCode();
        //2.存入reids key value 过期时间
        String key = RedisPrefixUtils.generatorKeyByPhone(passengerPhone);
        stringRedisTemplate.opsForValue().set(key,numberCode+"",2, TimeUnit.MINUTES);
        //3.通过短信服务商，将对应的验证码发送到手机上，阿里短信服务，腾讯短信通
        log.info(key);
        log.info(passengerPhone+":"+numberCode);
        return ResponseResult.success("");
    }

    @Override
    public ResponseResult checkVerificationCode(String passengerPhone, String verificationCode) {
        log.info("手机号："+passengerPhone+"," +"验证码："+verificationCode);
        //1.根据手机号去redis里面查找验证码
        String key = RedisPrefixUtils.generatorKeyByPhone(passengerPhone);
        String codeRedis = stringRedisTemplate.opsForValue().get(key);
        log.info("redis中的验证码："+codeRedis);
        //2.校验验证码
        if (StringUtils.isBlank(codeRedis)){
            return ResponseResult.fail(CommonStatusEnum.VERIFICATION_CODE_ERROR.getCode(),
                    CommonStatusEnum.VERIFICATION_CODE_ERROR.getValue());
        }
        if (!verificationCode.trim().equals(codeRedis.trim())){
            return ResponseResult.fail(CommonStatusEnum.VERIFICATION_CODE_ERROR.getCode(),
                    CommonStatusEnum.VERIFICATION_CODE_ERROR.getValue());
        }
        //3.查找是否有当前用户 并完成登录或者注册
        VerificationCodeDTO codeDTO = new VerificationCodeDTO();
        codeDTO.setPassengerPhone(passengerPhone);
        servicePassengerUserFeign.loginOrReg(codeDTO);
        //4.生成token 颁发令牌
        String accessToken = JwtUtils.generatorToken(passengerPhone, IdentityConstants.PASSENGER_IDENTITY, TokenConstants.ACCESS_TOKEN_TYPE);
        String refreshToken = JwtUtils.generatorToken(passengerPhone, IdentityConstants.PASSENGER_IDENTITY, TokenConstants.REFRESH_TOKEN_TYPE);

        String accessTokenKey = RedisPrefixUtils.generatorTokenKey(passengerPhone, IdentityConstants.PASSENGER_IDENTITY, TokenConstants.ACCESS_TOKEN_TYPE);
        stringRedisTemplate.opsForValue().set(accessTokenKey,accessToken,30,TimeUnit.DAYS);

        String refreshTokenKey = RedisPrefixUtils.generatorTokenKey(passengerPhone, IdentityConstants.PASSENGER_IDENTITY, TokenConstants.REFRESH_TOKEN_TYPE);
        stringRedisTemplate.opsForValue().set(refreshTokenKey,refreshToken,31,TimeUnit.DAYS);
        TokenResponse tokenResponse = new TokenResponse();
        tokenResponse.setAccessToken(accessToken);
        tokenResponse.setRefreshToken(refreshToken);
        log.info("accessToken key:{},refreshTokenKey:{}",accessTokenKey,refreshTokenKey);
        log.info("用户{}accessToken：{},refreshToken:{}",passengerPhone,accessToken,refreshToken);
        //5.返回
        return ResponseResult.success(tokenResponse);
    }
}
